home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2003 November A / PCWK1103A.iso / ABBYY FineReader 7.0 PRO / data1.cab / gs_fonts.ps < prev    next >
Text File  |  2002-10-23  |  34KB  |  1,046 lines

  1. %    Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_fonts.ps,v 1.13.2.3 2002/04/02 13:57:27 mpsuzuki Exp $
  14. % Font initialization and management code.
  15.  
  16. % ATAPY changes at 23.10.2002. Comments about changes started with string "(ATAPY changes)"
  17.  
  18. % Define the default font.
  19. /defaultfontname /Courier def
  20.  
  21. % Define the name of the font map file.
  22. /defaultfontmap (Fontmap) def
  23.  
  24. % ------ End of editable parameters ------ %
  25.  
  26. % Define the UniqueIDs and organization XUID assigned to Aladdin.
  27. % UniqueIDs 5,066,501 - 5,066,580 are assigned as follows:
  28. %   01 and 02 for shareware Cyrillic
  29. %   33 through 67 for Type 1 versions of the Hershey fonts
  30. % UniqueIDs 5,115,501 - 5,115,600 are currently unassigned.
  31. /AladdinEnterprisesXUID 107 def
  32.  
  33. % If SUBSTFONT is defined, make it the default font.
  34. /SUBSTFONT where { pop /defaultfontname /SUBSTFONT load def } if
  35.  
  36. % Define a reliable way of accessing FontDirectory in systemdict.
  37. /.FontDirectory
  38. { /FontDirectory .systemvar
  39. } .bind odef
  40.  
  41. % If DISKFONTS is true, we load individual CharStrings as they are needed.
  42. % (This is intended primarily for machines with very small memories.)
  43. % In this case, we define another dictionary, parallel to FontDirectory,
  44. % that retains an open file for every font loaded.
  45. /FontFileDirectory 10 dict def
  46.  
  47. % Define a temporary string for local use, since using =string
  48. % interferes with some PostScript programs.
  49. /.fonttempstring 128 string def
  50.  
  51. % Split up a search path into individual directories or files.
  52. /.pathlist        % <path> .pathlist <dir1|file1> ...
  53.  {  { dup length 0 eq { pop exit } if
  54.       .filenamelistseparator search not { exit } if
  55.       exch pop exch
  56.     }
  57.    loop
  58.  } bind def
  59.  
  60. % Load a font name -> font file name map.
  61. userdict /Fontmap .FontDirectory maxlength dict put
  62. /.loadFontmap {        % <file> .loadFontmap -
  63.         % We would like to simply execute .definefontmap as we read,
  64.         % but we have to maintain backward compatibility with an older
  65.         % specification that makes later entries override earlier
  66.         % ones within the same file.
  67.    50 dict exch .readFontmap
  68.     { .definefontmap } forall
  69. } bind def
  70. /.readFontmap {        % <dict> <file> .readFontmap <dict>
  71.     { dup token not { closefile exit } if
  72.         % stack: dict file fontname
  73.       % This is a hack to get around the absurd habit of MS-DOS editors
  74.       % of adding an EOF character at the end of the file.
  75.       dup (\032) eq { pop closefile exit } if
  76.       1 index token not
  77.        { (Fontmap entry for ) print dup =only
  78.      ( has no associated file or alias name!  Giving up.) = flush
  79.      {.readFontmap} 0 get 1 .quit
  80.        } if
  81.       dup type dup /stringtype eq exch /nametype eq or not
  82.        { (Fontmap entry for ) print 1 index =only
  83.      ( has an invalid file or alias name!  Giving up.) = flush
  84.      {.readFontmap} 0 get 1 .quit
  85.        } if
  86.         % stack: dict file fontname filename|aliasname
  87.       1 index type /stringtype eq
  88.       1 index type /nametype eq and 1 index xcheck and
  89.       1 index /run eq 2 index /.runlibfile eq or and {
  90.         % This is an inclusion entry.
  91.     pop findlibfile { exch pop } { file } ifelse
  92.     2 index exch .readFontmap pop
  93.       } {
  94.         % This is a real entry.
  95.         % Read and pop tokens until a semicolon.
  96.        { 2 index token not
  97.       { (Fontmap entry for ) print 1 index =only
  98.         ( ends prematurely!  Giving up.) = flush
  99.         {.loadFontmap} 0 get 1 .quit
  100.       } if
  101.      dup /; eq { pop 3 index 3 1 roll .growput exit } if
  102.      pop
  103.        } loop
  104.       } ifelse
  105.     } loop
  106. } bind def
  107. % Add an entry in Fontmap.  We redefine this if the Level 2
  108. % resource machinery is loaded.
  109. /.definefontmap            % <fontname> <file|alias> .definefontmap -
  110.  {        % Since Fontmap is global, make sure the values are storable.
  111.    .currentglobal 3 1 roll true .setglobal
  112.    dup type /stringtype eq
  113.     { dup .gcheck not { dup length string copy } if
  114.     }
  115.    if
  116.    Fontmap 3 -1 roll 2 copy .knownget
  117.     {        % Add an element to the end of the existing value,
  118.         % unless it's the same as the current last element.
  119.       mark exch aload pop counttomark 4 add -1 roll
  120.       2 copy eq { cleartomark pop pop } { ] readonly .growput } ifelse
  121.     }
  122.     {        % Make a new entry.
  123.       mark 4 -1 roll ] readonly .growput
  124.     }
  125.    ifelse .setglobal
  126.  } bind def
  127.  
  128. % Parse a font file just enough to find the FontName or FontType.
  129. /.findfontvalue {    % <file> <key> .findfontvalue <value> true
  130.             % <file> <key> .findfontvalue false
  131.             % Closes the file in either case.
  132.   exch dup read not { -1 } if
  133.   2 copy unread 16#80 eq {
  134.     dup (xxxxxx) readstring pop pop        % skip .PFB header
  135.   } if
  136.   {        % Stack: key file
  137.         % Protect ourselves against syntax errors here.
  138.     dup { token } stopped { pop false exit } if
  139.     not { false exit } if        % end of file
  140.     dup /eexec eq { pop false exit } if        % reached eexec section
  141.     dup /Subrs eq { pop false exit } if        % Subrs without eexec
  142.     dup /CharStrings eq { pop false exit } if    % CharStrings without eexec
  143.     dup 3 index eq
  144.      { xcheck not { dup token exit } if }    % found key
  145.      { pop }
  146.     ifelse
  147.   } loop
  148.         % Stack: key file value true   (or)
  149.         % Stack: key file false
  150.   dup { 4 } { 3 } ifelse -2 roll closefile pop
  151. } bind def
  152. /.findfontname
  153.  { /FontName .findfontvalue
  154.  } bind def
  155.  
  156. % If there is no FONTPATH, try to get one from the environment.
  157. NOFONTPATH { /FONTPATH () def } if
  158. /FONTPATH where
  159.  { pop }
  160.  { /FONTPATH (GS_FONTPATH) getenv not { () } if def }
  161. ifelse
  162. FONTPATH length 0 eq { (%END FONTPATH) .skipeof } if
  163. /FONTPATH [ FONTPATH .pathlist ] def
  164.  
  165. % Scan directories looking for plausible fonts.  "Plausible" means that
  166. % the file begins with %!PS-AdobeFont or %!FontType1, or with \200\001
  167. % followed by four arbitrary bytes and then either of these strings.
  168. % To speed up the search, we skip any file whose name appears in
  169. % the Fontmap (with any extension and upper/lower case variation) already,
  170. % and any file whose extension definitely indicates it is not a font.
  171. %
  172. % NOTE: The current implementation of this procedure is somewhat Unix/DOS-
  173. % specific.  It assumes that '/' and '\' are directory separators, and that
  174. % the part of a file name following the last '.' is the extension.
  175. %
  176. /.lowerstring        % <string> .lowerstring <lowerstring>
  177.  { 0 1 2 index length 1 sub
  178.     { 2 copy get dup 65 ge exch 90 le and
  179.        { 2 copy 2 copy get 32 add put }
  180.      if pop
  181.     }
  182.    for
  183.  } bind def
  184. /.splitfilename {    % <dir.../base.extn> .basename <base> <extn>
  185.         % Make the file name read-only to detect aliasing bugs.
  186.         % We really don't like doing this, but we've had one
  187.         % such bug already.
  188.   readonly {
  189.     (/) search { true } { (\\) search } ifelse { pop pop } { exit } ifelse
  190.   } loop
  191.   dup { (.) search { pop pop } { exit } ifelse } loop
  192.   2 copy eq {
  193.     pop ()
  194.   } {
  195.     exch dup length 2 index length 1 add sub 0 exch getinterval exch
  196.   } ifelse
  197. } bind def
  198. /.scanfontdict 1 dict def        % establish a binding
  199. /.scanfontbegin
  200.  {    % Construct the table of all file names already in Fontmap.
  201.    currentglobal true setglobal
  202.    .scanfontdict dup maxlength Fontmap length 2 add .max .setmaxlength
  203.    Fontmap
  204.     { exch pop
  205.        { dup type /stringtype eq
  206.       { .splitfilename pop .fonttempstring copy .lowerstring cvn
  207.         .scanfontdict exch true put
  208.       }
  209.       { pop
  210.       }
  211.      ifelse
  212.        }
  213.       forall
  214.     }
  215.    forall
  216.    setglobal
  217.  } bind def
  218. /.scanfontskip mark
  219.         % Strings are converted to names anyway, so....
  220.   /afm true
  221.   /bat true
  222.   /c true
  223.   /cmd true
  224.   /com true
  225.   /dir true
  226.   /dll true
  227.   /doc true
  228.   /drv true
  229.   /exe true
  230.   /fon true
  231.   /fot true
  232.   /h true
  233.   /o true
  234.   /obj true
  235.   /pfm true
  236.   /pss true        % Adobe Multiple Master font instances
  237.   /txt true
  238. .dicttomark def
  239. /.scan1fontstring 128 string def
  240. % %%BeginFont: is not per Adobe documentation, but a few fonts have it.
  241. /.scanfontheaders [(%!PS-Adobe*) (%!FontType*) (%%BeginFont:*)] def
  242. 0 .scanfontheaders { length .max } forall 6 add    % extra for PFB header
  243. /.scan1fontfirst exch string def
  244. /.scanfontdir        % <dirname> .scanfontdir -
  245.  { currentglobal exch true setglobal
  246.    QUIET not { (Scanning ) print dup print ( for fonts...) print flush } if
  247.    (*) 1 index .filenamedirseparator
  248.    dup (\\) eq { pop (\\\\) } if    % double \ for pattern match
  249.    exch concatstrings concatstrings
  250.    0 0 0 4 -1 roll    % found scanned files
  251.     {        % stack: <fontcount> <scancount> <filecount> <filename>
  252.       exch 1 add exch                   % increment filecount
  253.       dup .splitfilename .fonttempstring copy .lowerstring
  254.         % stack: <fontcount> <scancount> <filecount+1> <filename>
  255.         %    <BASE> <ext>
  256.       .scanfontskip exch known exch .scanfontdict exch known or
  257.        { pop
  258.         % stack: <fontcount> <scancount> <filecount+1>
  259.        }
  260.        { 3 -1 roll 1 add 3 1 roll
  261.         % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  262.      dup (r) { file } .internalstopped
  263.       { pop pop null ()
  264.         % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  265.         %    null ()
  266.       }
  267.       {
  268.         % On some platforms, the file operator will open directories,
  269.         % but an error will occur if we try to read from one.
  270.         % Handle this possibility here.
  271.         dup .scan1fontfirst { readstring } .internalstopped
  272.          { pop pop () }
  273.          { pop }
  274.         ifelse
  275.         % stack: <fontcount> <scancount+1> <filecount+1>
  276.         %    <filename> <file> <header>
  277.       }
  278.      ifelse
  279.         % Check for PFB file header.
  280.      dup (\200\001????*) .stringmatch
  281.       { dup length 6 sub 6 exch getinterval }
  282.      if
  283.         % Check for font file headers.
  284.      false .scanfontheaders
  285.       { 2 index exch .stringmatch or
  286.       }
  287.      forall exch pop
  288.       {    % stack: <fontcount> <scancount+1> <filecount+1> <filename>
  289.         %    <file>
  290.         dup 0 setfileposition .findfontname
  291.          { dup Fontmap exch known
  292.         { pop pop
  293.         }
  294.         { exch copystring exch
  295.           DEBUG { ( ) print dup =only flush } if
  296.           1 index .definefontmap
  297.           .splitfilename pop true .scanfontdict 3 1 roll .growput
  298.             % Increment fontcount.
  299.           3 -1 roll 1 add 3 1 roll
  300.         }
  301.            ifelse
  302.          }
  303.          { pop
  304.          }
  305.         ifelse
  306.       }
  307.         % .findfontname will have done a closefile in the above case.
  308.       { dup null eq { pop } { closefile } ifelse pop
  309.       }
  310.      ifelse
  311.        }
  312.       ifelse
  313.     }
  314.    .scan1fontstring filenameforall
  315.    QUIET
  316.     { pop pop pop }
  317.     { ( ) print =only ( files, ) print =only ( scanned, ) print
  318.       =only ( new fonts.) = flush
  319.     }
  320.    ifelse
  321.    setglobal
  322.  } bind def
  323.  
  324. %END FONTPATH
  325.  
  326. % Create the dictionary that registers the .buildfont procedure (called by
  327. % definefont) for each FontType.
  328. /buildfontdict 20 dict def
  329.  
  330. % Register Type 3 fonts, which are always supported, for definefont.
  331. buildfontdict 3 /.buildfont3 cvx put
  332.  
  333. % Register Type 0 fonts if they are supported.  Strictly speaking,
  334. % we should do this in its own file (gs_type0.ps), but since this is
  335. % the only thing that would be in that file, it's simpler to put it here.
  336. /.buildfont0 where { pop buildfontdict 0 /.buildfont0 cvx put } if
  337.  
  338. % Define definefont.  This is a procedure built on a set of operators
  339. % that do all the error checking and key insertion.
  340. /.growfontdict
  341.  {    % Grow the font dictionary, if necessary, to ensure room for an
  342.     % added entry, making sure there is at least one slot left for FID.
  343.    dup maxlength 1 index length sub 2 lt
  344.     { dup dup wcheck
  345.        { .growdict }
  346.        { .growdictlength dict .copydict }
  347.       ifelse
  348.     }
  349.     { dup wcheck not { dup maxlength dict .copydict } if
  350.     }
  351.    ifelse
  352.  } bind def 
  353. /.completefont {
  354.   {        % Check for disabled platform fonts.
  355.       NOPLATFONTS
  356.        {    % Make sure we leave room for FID.
  357.      .growfontdict dup /ExactSize 0 put
  358.        }
  359.        {    % Hack: if the Encoding looks like it might be the
  360.         % Symbol or Dingbats encoding, load those now (for the
  361.         % benefit of platform font matching) just in case
  362.         % the font didn't actually reference them.
  363.         % Note that some types of font don't have an Encoding.
  364.      dup /Encoding .knownget {
  365.        dup length 65 ge {
  366.          64 get
  367.          dup /congruent eq { SymbolEncoding pop } if
  368.          /a9 eq { DingbatsEncoding pop } if
  369.        } {
  370.          pop
  371.        } ifelse
  372.      } if
  373.        }
  374.       ifelse
  375.       true exch
  376.         % If this is a CIDFont, CIDFontType takes precedence
  377.         % over FontType.
  378.       dup /CIDFontType known {
  379.     /.buildcidfont where {
  380.       pop exch not exch    % true => false
  381.     } if
  382.       } if
  383.       exch {
  384.     dup /FontType get //buildfontdict exch get exec
  385.       } {
  386.     .buildcidfont
  387.       } ifelse
  388.  
  389.       DISKFONTS
  390.        { FontFileDirectory 2 index known
  391.       { dup /FontFile FontFileDirectory 4 index get .growput
  392.       }
  393.      if
  394.        }
  395.       if
  396.       readonly        % stack: name fontdict
  397.   } stopped { /invalidfont signalerror } if
  398. } bind odef
  399. /definefont
  400.  { .completefont
  401.         % If the current allocation mode is global, also enter
  402.         % the font in LocalFontDirectory.
  403.    .currentglobal
  404.     { //systemdict /LocalFontDirectory .knownget
  405.        { 2 index 2 index .growput }
  406.       if
  407.     }
  408.    if
  409.    dup .FontDirectory 4 -2 roll .growput
  410.         % If the font originated as a resource, register it.
  411.    currentfile .currentresourcefile eq { dup .registerfont } if
  412.  } odef
  413.  
  414. % Define a procedure for defining aliased fonts.
  415. % We use this only for explicitly aliased fonts, not substituted fonts:
  416. % we think this matches the observed behavior of Adobe interpreters.
  417. /.aliasfont        % <name> <font> .aliasfont <newFont>
  418.  { .currentglobal 3 1 roll dup .gcheck .setglobal
  419.    dup length 2 add dict
  420.    dup 3 -1 roll { 1 index /FID eq { pop pop } { put dup } ifelse } forall
  421.         % Stack: global fontname newfont newfont.
  422.         % We might be defining a global font whose FontName
  423.         % is a local string.  This is weird, but legal,
  424.         % and doesn't cause problems anywhere else:
  425.         % to avoid any possible problems in this case, do a cvn.
  426.         % We might also be defining (as an alias) a global font
  427.         % whose FontName is a local non-string, if someone passed a
  428.         % garbage value to findfont.  In this case, just don't
  429.         % call definefont at all.
  430.    2 index dup type /stringtype eq exch .gcheck or 1 index .gcheck not or
  431.     { /FontName 3 index dup type /stringtype eq { cvn } if put
  432.         % Don't bind in definefont, since Level 2 redefines it.
  433.       /definefont .systemvar exec
  434.     }
  435.     { .completefont pop exch pop
  436.     }
  437.    ifelse exch .setglobal
  438.  } odef        % so findfont will bind it
  439.  
  440. % Define .loadfontfile for loading a font.  If we recognize Type 1 and/or
  441. % TrueType fonts, gs_type1.ps and/or gs_ttf.ps will redefine this.
  442. /.loadfontfile {
  443.         % According to Ed Taft, Adobe interpreters push userdict
  444.         % before loading a font, and pop it afterwards.
  445.   userdict begin
  446.     cvx exec
  447.   end
  448. } bind def
  449. /.loadfont
  450.  {        % Some buggy fonts leave extra junk on the stack,
  451.         % so we have to make a closure that records the stack depth
  452.         % in a fail-safe way.
  453.    {{.loadfontfile} .execasresource} count 1 sub 2 .execn
  454.    count exch sub { pop } repeat
  455.  } bind def
  456.  
  457. % Find an alternate font to substitute for an unknown one.
  458. % We go to some trouble to parse the font name and extract
  459. % properties from it.  Later entries take priority over earlier.
  460.  
  461. % (ATAPY changes) We commented out and changed some inappropriate substitutions
  462.  
  463. /.substitutefaces [
  464.     % Guess at suitable substitutions for random unknown fonts.
  465.   [(Book) /NewCenturySchlbk 0]
  466.   %[(Grot) /Helvetica 0]
  467.   [(Roman) /Times 0]
  468.   [(Chancery) /ZapfChancery-MediumItalic 0]
  469.     % If the family name appears in the font name,
  470.     % use a font from that family.
  471.   [(Arial) /Helvetica 0]
  472.   [(Avant) /AvantGarde 0]
  473.   [(Bookman) /Bookman 0]
  474.   [(Century) /NewCenturySchlbk 0]
  475.   [(Cour) /Courier 0]
  476.   [(Frut) /Helvetica 0]
  477.   [(Garamond) /Times 0]
  478.   [(Geneva) /Helvetica 0]
  479.   [(Helv) /Helvetica 0]
  480.   [(NewYork) /Bookman 0]
  481.   [(Pala) /Palatino 0]
  482.   [(Schlbk) /NewCenturySchlbk 0]
  483.   [(Swiss) /Helvetica 0]
  484.   [(Symbol) /Symbol 0]
  485.   [(Times) /Times 0]
  486.     % Substitute for Adobe Multiple Master fonts.
  487.   [(Minion) /Times 0]
  488.   [(Myriad) /Helvetica 0]
  489.     % If the font wants to be monospace, use Courier.
  490.   [(Monospace) /Courier 0]
  491.   [(Typewriter) /Courier 0]
  492.     % Define substitutes for the other Adobe PostScript 3 fonts.
  493.     % For some of them, the substitution is pretty bad!
  494.   [(Albertus) /Palatino 0]
  495.   [(AntiqueOlive) /Helvetica 0]
  496.   [(Bodoni) /NewCenturySchlbk 0]
  497.   %[(Chicago) /Helvetica 2]
  498.   [(Clarendon) /Bookman 0]
  499.   [(Cooper) /NewCenturySchlbk 0]
  500.   [(Copperplate) /AvantGarde 0]    % inappropriate, small-cap font
  501.   [(Coronet) /ZapfChancery-MediumItalic 0]
  502.   [(Eurostile) /Helvetica 0]
  503.   %[(Geneva) /Courier 2]        % should be fixed-pitch sans demi
  504.   %[(GillSans) /Helvetica 2]
  505.   [(GillSans-Light) /Helvetica 0]
  506.   [(Goudy) /Palatino 0]
  507.   [(Hoefler) /NewCenturySchlbk 0]
  508.   [(Joanna) /Times 0]
  509.   [(LetterGothic) /Courier 0]    % should be fixed-pitch sans
  510.   %[(LubalinGraph-Book) /Bookman 2]
  511.   [(LubalinGraph-Demi) /Bookman 0]
  512.   [(Marigold) /ZapfChancery-MediumItalic 0]
  513.   [(MonaLisa-Recut) /Palatino 0]    % inappropriate
  514.   %[(Monaco) /Courier 2]        % should be fixed-pitch sans demi
  515.   [(Optima) /Helvetica 0]
  516.   [(Oxford) /ZapfChancery-MediumItalic 0]
  517.   [(Tekton) /Helvetica 0]
  518.   [(Univers) /Helvetica 0]
  519. ] readonly def
  520. /.substituteproperties [
  521.   [(It) 1] [(Oblique) 1]
  522.   [(Black) 2] [(Bd) 2] [(Bold) 2] [(bold) 2] [(Demi) 2] [(Heavy) 2] [(Sb) 2]
  523.   [(Cn) 4] [(Cond) 4] [(Narrow) 4] [(Pkg) 4] [(Compr) 4]
  524.   [(Serif) 8] [(Sans) -8]
  525. ] readonly def
  526. /.fontnameproperties {        % <int> <string|name> .fontnameproperties
  527.                 %   <int'>
  528.   .fontnamestring
  529.   .substituteproperties {
  530.     2 copy 0 get search {
  531.       pop pop pop dup length 1 sub 1 exch getinterval 3 -1 roll exch {
  532.     dup 0 ge { or } { neg not and } ifelse
  533.       } forall exch
  534.     } {
  535.       pop pop
  536.     } ifelse
  537.   } forall pop
  538. } bind def
  539. /.substitutefamilies mark
  540.   /AvantGarde
  541.     {/AvantGarde-Book /AvantGarde-BookOblique
  542.      /AvantGarde-Demi /AvantGarde-DemiOblique}
  543.   /Bookman
  544.     {/Bookman-Demi /Bookman-DemiItalic /Bookman-Light /Bookman-LightItalic}
  545.   /Courier
  546.     {/Courier /Courier-Oblique /Courier-Bold /Courier-BoldOblique}
  547.   /Helvetica
  548.     {/Helvetica /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
  549.      /Helvetica-Narrow /Helvetica-Narrow-Oblique
  550.      /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique}
  551.   /NewCenturySchlbk
  552.     {/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
  553.      /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic}
  554.   /Palatino
  555.     {/Palatino-Roman /Palatino-Italic /Palatino-Bold /Palatino-BoldItalic}
  556.   /Symbol
  557.     {/Symbol /Symbol /Symbol /Symbol}
  558.   /Times
  559.     {/Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic}
  560.   /ZapfChancery-MediumItalic
  561.     {/ZapfChancery-MediumItalic}
  562. .dicttomark readonly def
  563. /.nametostring {        % <name> .nametostring <string>
  564.                 % <other> .nametostring <other>
  565.   dup type /nametype eq { .namestring } if
  566. } bind def
  567. /.fontnamestring {        % <fontname> .fontnamestring <string|name>
  568.   dup type dup /nametype eq {
  569.     pop .namestring
  570.   } {
  571.     /stringtype ne { pop () } if
  572.   } ifelse
  573. } bind def
  574. /.substitutefontname {        % <fontname> <properties> .substitutefontname
  575.                 %   <altname|null>
  576.     % Look for properties and/or a face name in the font name.
  577.     % If we find any, use Times (serif) or Helvetica (sans) as the
  578.     % base font; otherwise, use the default font.
  579.     % Note that the "substituted" font name may be the same as
  580.     % the requested one; the caller must check this.
  581.   exch .fontnamestring {
  582.     defaultfontname /Helvetica-Oblique /Helvetica-Bold /Helvetica-BoldOblique
  583.     /Helvetica-Narrow /Helvetica-Narrow-Oblique
  584.     /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  585.     /Times-Roman /Times-Italic /Times-Bold /Times-BoldItalic
  586.     /Helvetica-Narrow /Helvetica-Narrow-Oblique
  587.     /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  588.   } 3 1 roll
  589.     % Stack: facelist properties fontname
  590.     % Look for a face name.
  591.   .substitutefaces {
  592.     2 copy 0 get search {
  593.       pop pop pop
  594.     % Stack: facelist properties fontname [(pattern) family properties]
  595.       dup 2 get 4 -1 roll or 3 1 roll
  596.       1 get .substitutefamilies exch get
  597.       4 -1 roll pop 3 1 roll
  598.     } {
  599.       pop pop
  600.     } ifelse
  601.   } forall pop
  602.   1 index length mod get exec
  603. } bind def
  604. /.substitutefont {        % <fontname> .substitutefont <altname>
  605.   dup 0 exch .fontnameproperties .substitutefontname
  606.     % Only accept fonts known in the Fontmap.
  607.    Fontmap 1 index known not { pop defaultfontname } if
  608. } bind def
  609.  
  610. % If requested, make (and recognize) fake entries in FontDirectory for fonts
  611. % present in Fontmap but not actually loaded.  Thanks to Ray Johnston for
  612. % the idea behind this code.
  613. FAKEFONTS not { (%END FAKEFONTS) .skipeof } if
  614.  
  615. % We use the presence or absence of the FontMatrix key to indicate whether
  616. % a font is real or fake.  We must pop the arguments at the very end,
  617. % so that stack protection will be effective.
  618.  
  619. /definefont {        % <name> <font> definefont <font>
  620.   dup /FontMatrix known {
  621.     //definefont
  622.   } {
  623.     2 copy /FontName get findfont //definefont exch pop exch pop
  624.   } ifelse
  625. } bind odef
  626.  
  627. /scalefont {        % <font> <scale> scalefont <font>
  628.   1 index /FontMatrix known {
  629.     //scalefont
  630.   } {
  631.     1 index /FontName get findfont 1 index //scalefont
  632.     exch pop exch pop
  633.   } ifelse
  634. } bind odef
  635.  
  636. /makefont {        % <font> <matrix> makefont <font>
  637.   1 index /FontMatrix known {
  638.     //makefont
  639.   } {
  640.     1 index /FontName get findfont 1 index //makefont
  641.     exch pop exch pop
  642.   } ifelse
  643. } bind odef
  644.  
  645. /setfont {        % <font> setfont -
  646.   dup /FontMatrix known {
  647.     //setfont
  648.   } {
  649.     dup /FontName get findfont //setfont pop
  650.   } ifelse
  651. } bind odef
  652.  
  653. %END FAKEFONTS
  654.  
  655. % Define findfont so it tries to load a font if it's not found.
  656. % The Red Book requires that findfont be a procedure, not an operator,
  657. % but it still needs to restore the stacks reliably if it fails,
  658. % so we do all the work in an operator.
  659. /.findfont {
  660.   mark 1 index
  661.   //systemdict begin .dofindfont
  662.     % Define any needed aliases.
  663.   counttomark 1 sub { .aliasfont } repeat end
  664.   exch pop exch pop
  665. } odef
  666. /findfont {
  667.   .findfont
  668. } bind def
  669. % Check whether the font name we are about to look for is already on the list
  670. % of aliases we're accumulating; if so, cause an error.
  671. /.checkalias        % -mark- <alias1> ... <name> .checkalias <<same>>
  672.  { counttomark 1 sub -1 1
  673.     { index 1 index eq
  674.        { pop QUIET not
  675.       { (Unable to substitute for font.) = flush
  676.       } if
  677.      /findfont cvx /invalidfont signalerror
  678.        }
  679.       if
  680.     }
  681.    for
  682.  } bind def
  683. % Get a (non-fake) font if present in a FontDirectory.
  684. /.fontknownget        % <fontdir> <fontname> .fontknownget <font> true
  685.             % <fontdir> <fontname> .fontknownget false
  686.  { .knownget
  687.     { FAKEFONTS
  688.        { dup /FontMatrix known { true } { pop false } ifelse }
  689.        { true }
  690.       ifelse
  691.     }
  692.     { false
  693.     }
  694.    ifelse
  695.  } bind def
  696. % This is the standard procedure for handling font substitution.
  697. % Its location is per an Adobe newsgroup posting.
  698. % It is called with the font name on the stack, standing in for findfont.
  699. /.stdsubstfont {    % mark <alias1> ... <fontname> .stdsubstfont <font>
  700.       /SUBSTFONT where {
  701.     pop QUIET not {
  702.       (Substituting for font ) print dup =only
  703.       (.) = flush
  704.     } if
  705.             % No aliasing.
  706.     cleartomark mark defaultfontname
  707.       } {
  708.     dup .substitutefont
  709.     2 copy eq { pop defaultfontname } if
  710.     .checkalias
  711.     QUIET not {
  712.       SHORTERRORS {
  713.         (%%[) print 1 index =only
  714.         ( not found, substituting ) print dup =only (]%%)
  715.       } {
  716.         (Substituting font ) print dup =only
  717.         ( for ) print 1 index =only (.)
  718.       } ifelse = flush
  719.     } if
  720.             % Remove all the accumulated aliases.
  721.     counttomark 1 add 1 roll cleartomark mark exch
  722.       } ifelse
  723.   .dofindfont
  724. } bind def
  725. $error /SubstituteFont { .stdsubstfont } put
  726. % Scan the next directory on FONTPATH.
  727. /.scannextfontdir {    % - .scannextfontdir <bool>
  728.             % If we haven't scanned all the directories in
  729.             % FONTPATH, scan the next one.
  730.   null 0 1 FONTPATH length 1 sub {
  731.     FONTPATH 1 index get null ne { exch pop exit } if pop
  732.   } for dup null ne {
  733.     dup 0 eq { .scanfontbegin } if
  734.     FONTPATH 1 index get .scanfontdir
  735.     FONTPATH exch null put true
  736.   } {
  737.     pop false
  738.   } ifelse
  739. } bind def
  740. % Do the work of findfont, including substitution, defaulting, and
  741. % scanning of FONTPATH.
  742. /.dofindfont {        % <fontname> .dofindfont <font>
  743.   .tryfindfont not {
  744.             % We didn't find the font.  If we haven't scanned
  745.             % all the directories in FONTPATH, scan the next one
  746.             % now and look for the font again.
  747.     .scannextfontdir {
  748.             % Start over with an empty alias list.
  749.       counttomark 1 sub { pop } repeat
  750.       .dofindfont
  751.     } {
  752.             % No luck.  Make sure we're not already
  753.             % looking for the default font.
  754.       dup defaultfontname eq {
  755.     QUIET not {
  756.       (Unable to load default font ) print
  757.       dup =only (!  Giving up.) = flush
  758.     } if
  759.     /findfont cvx /invalidfont signalerror
  760.       } if
  761.             % Substitute for the font.  Don't alias.
  762.       $error /SubstituteFont get exec
  763.     } ifelse
  764.   } if
  765. } bind def
  766. % Try to find a font using only the present contents of Fontmap.
  767. /.tryfindfont {        % <fontname> .tryfindfont <font> true
  768.             % <fontname> .tryfindfont false
  769.   .FontDirectory 1 index .fontknownget
  770.     {            % Already loaded
  771.       exch pop true
  772.     }
  773.     { dup Fontmap exch .knownget not
  774.        {        % Unknown font name.  Look for a file with the
  775.             % same name as the requested font.
  776.      .tryloadfont
  777.        }
  778.        {        % Try each element of the Fontmap in turn.
  779.      false exch    % (in case we exhaust the list)
  780.             % Stack: fontname false fontmaplist
  781.      { exch pop
  782.        dup type /nametype eq
  783.         {            % Font alias
  784.           .checkalias .tryfindfont exit
  785.         }
  786.         { dup dup type dup /arraytype eq exch /packedarraytype eq or exch xcheck and
  787.            {        % Font with a procedural definition
  788.          exec        % The procedure will load the font.
  789.                 % Check to make sure this really happened.
  790.          .FontDirectory 1 index .knownget
  791.           { exch pop true exit }
  792.          if
  793.            }
  794.            {        % Font file name
  795.          .loadfontloop { true exit } if
  796.            }
  797.           ifelse
  798.         }
  799.        ifelse false
  800.      }
  801.      forall
  802.             % Stack: font true -or- fontname false
  803.      { true
  804.      }
  805.      {            % None of the Fontmap entries worked.
  806.                 % Try loading a file with the same name
  807.                 % as the requested font.
  808.        .tryloadfont
  809.      }
  810.     ifelse
  811.        }
  812.       ifelse
  813.     }
  814.    ifelse
  815.  } bind def
  816.  
  817. % Attempt to load a font from a file.
  818. /.tryloadfont {        % <fontname> .tryloadfont <font> true
  819.             % <fontname> .tryloadfont false
  820.   dup .nametostring
  821.         % Hack: check for the presence of the resource machinery.
  822.   /.genericrfn where {
  823.     pop
  824.     2 copy .fonttempstring /FontResourceDir getsystemparam .genericrfn
  825.     .loadfontloop {
  826.       exch pop exch pop true
  827.     } {
  828.       dup .nametostring .loadfontloop
  829.     } ifelse
  830.   } {
  831.     .loadfontloop
  832.   } ifelse
  833. } bind def
  834. /.loadfontloop {    % <fontname> <filename> .loadfontloop
  835.             %   <font> true
  836.             % -or-
  837.             %   <fontname> false
  838.             % See above regarding the use of 'loop'.
  839.     {             % Is the font name a string?
  840.     dup type /stringtype ne
  841.      { QUIET not
  842.         { (Can't find font with non-string name: ) print dup =only (.) = flush
  843.         }
  844.        if pop false exit
  845.      }
  846.     if
  847.             % Can we open the file?
  848.     findlibfile not
  849.      { QUIET not
  850.         { (Can't find \(or can't open\) font file ) print dup print
  851.           (.) = flush
  852.         }
  853.        if pop false exit
  854.      }
  855.     if
  856.  
  857.             % Stack: fontname fontfilename fontfile
  858.     DISKFONTS
  859.      { .currentglobal true .setglobal
  860.        2 index (r) file
  861.        FontFileDirectory exch 5 index exch .growput
  862.        .setglobal
  863.      }
  864.     if
  865.     QUIET not
  866.      { (Loading ) print 2 index =only
  867.        ( font from ) print 1 index print (... ) print flush
  868.      }
  869.     if
  870.     % If LOCALFONTS isn't set, load the font into local or global
  871.     % VM according to FontType; if LOCALFONTS is set, load the font
  872.     % into the current VM, which is what Adobe printers (but not
  873.     % DPS or CPSI) do.
  874.     LOCALFONTS { false } { /setglobal where } ifelse
  875.      { pop /FontType .findfontvalue { 1 eq } { false } ifelse
  876.         % .setglobal, like setglobal, aliases FontDirectory to
  877.         % GlobalFontDirectory if appropriate.  However, we mustn't
  878.         % allow the current version of .setglobal to be bound in,
  879.         % because it's different depending on language level.
  880.        .currentglobal exch /.setglobal .systemvar exec
  881.         % Remove the fake definition, if any.
  882.        .FontDirectory 3 index .undef
  883.        1 index (r) file .loadfont .FontDirectory exch
  884.        /.setglobal .systemvar exec
  885.      }
  886.      { .loadfont .FontDirectory
  887.      }
  888.     ifelse
  889.         % Stack: fontname fontfilename fontdirectory
  890.     QUIET not
  891.      { //systemdict /level2dict known
  892.         { .currentglobal false .setglobal vmstatus
  893.           true .setglobal vmstatus 3 -1 roll pop
  894.           6 -1 roll .setglobal 5
  895.         }
  896.         { vmstatus 3
  897.         }
  898.        ifelse { =only ( ) print } repeat
  899.        (done.) = flush
  900.      } if
  901.  
  902.         % Check to make sure the font was actually loaded.
  903.     dup 3 index .fontknownget
  904.      { 4 1 roll pop pop pop true exit } if
  905.  
  906.         % Maybe the file had a different FontName.
  907.         % See if we can get a FontName from the file, and if so,
  908.         % whether a font by that name exists now.
  909.     exch (r) file .findfontname
  910.      { 2 copy .fontknownget
  911.         {    % Yes.  Stack: origfontname fontdirectory filefontname fontdict
  912.           3 -1 roll pop exch
  913.           QUIET
  914.            { pop
  915.            }
  916.            { (Using ) print =only
  917.              ( font for ) print 1 index =only
  918.              (.) = flush
  919.            }
  920.           ifelse true exit
  921.         }
  922.        if pop
  923.      }
  924.     if pop
  925.  
  926.         % The font definitely did not load correctly.
  927.     QUIET not
  928.      { (Loading ) print dup =only
  929.        ( font failed.) = flush
  930.      } if
  931.     false exit
  932.  
  933.     } loop        % end of loop
  934.  
  935.  } bind def
  936.  
  937. % Define a procedure to load all known fonts.
  938. % This isn't likely to be very useful.
  939. /loadallfonts
  940.  { Fontmap { pop findfont pop } forall
  941.  } bind def
  942.  
  943. % If requested, load all the fonts defined in the Fontmap into FontDirectory
  944. % as "fake" fonts i.e., font dicts with only FontName and FontType defined.
  945. % (We define FontType only for the sake of some questionable code in the
  946. % Apple Printer Utility 2.0 font inquiry code.)
  947. %
  948. % Note that this procedure only creates fake fonts in the FontDirectory
  949. % associated with the current VM.  This is because in multi-context systems,
  950. % creating the fake fonts in local VM leads to undesirable complications.
  951. /.definefakefonts
  952.     {
  953.     }
  954.     {
  955.       (gs_fonts FAKEFONTS) VMDEBUG
  956.       Fontmap {
  957.     pop dup type /stringtype eq { cvn } if
  958.     .FontDirectory 1 index known not {
  959.       2 dict dup /FontName 3 index put
  960.       dup /FontType 1 put
  961.       .FontDirectory 3 1 roll put
  962.     } {
  963.       pop
  964.     } ifelse
  965.       } forall
  966.     }
  967. FAKEFONTS { exch } if pop def    % don't bind, .current/setglobal get redefined
  968.  
  969. % Install initial fonts from Fontmap.
  970. /.loadinitialfonts
  971.  { NOFONTMAP not
  972.     { /FONTMAP where
  973.       { pop [ FONTMAP .pathlist ]
  974.          { dup VMDEBUG findlibfile
  975.         { exch pop .loadFontmap }
  976.         { /undefinedfilename signalerror }
  977.            ifelse
  978.          }
  979.       }
  980.       { LIBPATH
  981.          { defaultfontmap 1 index .filenamedirseparator
  982.            exch concatstrings concatstrings dup VMDEBUG
  983.            (r) { file } .internalstopped
  984.         { pop pop } { .loadFontmap } ifelse
  985.          }
  986.       }
  987.      ifelse forall
  988.     }
  989.    if
  990.    .definefakefonts    % current VM is global
  991.  } def            % don't bind, .current/setglobal get redefined
  992.  
  993. % ---------------- Synthetic font support ---------------- %
  994.  
  995. % Create a new font by modifying an existing one.  paramdict contains
  996. % entries with the same keys as the ones found in a Type 1 font;
  997. % it should also contain enough empty entries to allow adding the
  998. % corresponding non-overridden entries from the original font dictionary,
  999. % including FID.  If paramdict includes a FontInfo entry, this will
  1000. % also override the original font's FontInfo, entry by entry;
  1001. % again, it must contain enough empty entries.
  1002.  
  1003. % Note that this procedure does not perform a definefont.
  1004.  
  1005. /.makemodifiedfont    % <fontdict> <paramdict> .makemodifiedfont <fontdict'>
  1006.  { exch
  1007.     {            % Stack: destdict key value
  1008.       1 index /FID ne
  1009.        { 2 index 2 index known
  1010.       {        % Skip fontdict entry supplied in paramdict, but
  1011.             % handle FontInfo specially.
  1012.         1 index /FontInfo eq
  1013.          { 2 index 2 index get        % new FontInfo
  1014.            1 index                % old FontInfo
  1015.         {    % Stack: destdict key value destinfo key value
  1016.           2 index 2 index known
  1017.            { pop pop }
  1018.            { 2 index 3 1 roll put }
  1019.           ifelse
  1020.         }
  1021.            forall pop
  1022.          }
  1023.         if
  1024.       }
  1025.       {        % No override, copy the fontdict entry.
  1026.         2 index 3 1 roll put
  1027.         dup dup    % to match pop pop below
  1028.       }
  1029.      ifelse
  1030.        }
  1031.       if
  1032.       pop pop
  1033.     } forall
  1034.  } bind def
  1035.  
  1036. % Make a modified font and define it.  Note that unlike definefont,
  1037. % this does not leave the font on the operand stack.
  1038.  
  1039. /.definemodifiedfont    % <fontdict> <paramdict> .definemodifiedfont -
  1040.  { .makemodifiedfont
  1041.    dup /FontName get exch definefont pop
  1042.  } bind def
  1043.